import tkinter
from tkinter.messagebox import *
import os
import sys
from main_login import connect_wifi
from post_njdx import post_url
from 校园网项目.post_cmcc import post_cmcc

index = tkinter.Tk()  # 创建主窗口
index.title('校园网登录')  # 设置主窗口标题
index.geometry('500x400')  # 设置主窗口大小
index.maxsize(500, 300)
index.minsize(500, 300)


# 获取文件路径
def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)


canvas = tkinter.Canvas(index, width=500, height=300, bg=None)
image_file = tkinter.PhotoImage(file=resource_path("2.gif"))
image = canvas.create_image(250, 0, anchor='n', image=image_file)
canvas.pack()

account_lable = tkinter.Label(index, text='账号', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)
account_lable.place(relx=0.29, rely=0.4)
pasw_lable = tkinter.Label(index, text='密码', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)
pasw_lable.place(relx=0.29, rely=0.5)

account = tkinter.Entry(index, width=30, highlightthickness=1, highlightcolor='lightskyblue', relief='groove')
account.place(relx=0.4, rely=0.4)
password = tkinter.Entry(index, show='*', width=30, highlightthickness=1, highlightcolor='lightskyblue',
                         relief='groove')
password.place(relx=0.4, rely=0.5)

user = {"admin": "123456"}

var = tkinter.IntVar()
checkbutton = tkinter.Checkbutton(index, text="自动保存账号和密码", variable=var)
checkbutton.place(relx=0.4, rely=0.57)

# 单选框
var_radio = tkinter.StringVar()
var_radio.set('电信')  # 默认选中'电信'
radiobutton1 = tkinter.Radiobutton(index, text='电信', variable=var_radio, value='电信')
radiobutton1.place(relx=0.4, rely=0.65)
radiobutton2 = tkinter.Radiobutton(index, text='移动', variable=var_radio, value='移动')
radiobutton2.place(relx=0.5, rely=0.65)


def login_app():
    ac = account.get()
    ps = password.get()
    if ac == "":
        showinfo("错误信息", "学号未填写")
    elif ps == "":
        showinfo("错误信息", "密码未填写")
    else:
        if var.get() == 1:
            with open("userinfo.txt", "w") as f:
                f.write(ac + "\n" + ps + "\n")
        account.delete(0, 'end')
        password.delete(0, 'end')
        showinfo("提示", "登陆成功！即将连接校园网......")
        status_code = 404
        if var_radio.get() == '移动':
            if connect_wifi('CMCC-EDU'):
                status_code = post_cmcc(ac, ps)
        else:
            if connect_wifi('@f-Young'):
                status_code = post_url(ac, ps)
        if status_code == 200:
            showinfo("提示", '登陆成功，即将退出')
            index.destroy()
        else:
            showinfo("提示", '登录失败，请重新尝试')


def quit_app():
    if askokcancel('退出', '确定退出？'):
        index.destroy()


# 强登按钮处理函数
def qd_app():
    with open(resource_path('message.txt'), 'r', encoding='utf-8') as file:
        message = file.read()
    if askokcancel('免责声明', message):
        login_app()


loginBtn = tkinter.Button(index,
                          text='登录',
                          font=('宋体', 12),
                          width=4,
                          height=1,
                          command=login_app,
                          relief='solid',
                          bd=0.5,
                          bg='lightcyan')
loginBtn.place(relx=0.41,
               rely=0.75)
quitBtn = tkinter.Button(index,
                         text='退出',
                         font=('宋体', 12),
                         width=4,
                         height=1,
                         command=quit_app,
                         relief='solid',
                         bd=0.5,
                         bg='lightcyan')
quitBtn.place(relx=0.71,
              rely=0.75)

# 添加强登按钮
qdBtn = tkinter.Button(index,
                       text='强登',
                       font=('宋体', 12),
                       width=4,
                       height=1,
                       command=qd_app,
                       relief='solid',
                       bd=0.5,
                       bg='lightcyan')
qdBtn.place(relx=0.56,
            rely=0.75)

if os.path.exists("userinfo.txt"):
    with open("userinfo.txt", "r") as f:
        lines = f.readlines()
        account.insert(0, lines[0].strip())
        password.insert(0, lines[1].strip())

if __name__ == '__main__':
    index.mainloop()
